package com.study.modules.mini.wxjava;

import me.chanjar.weixin.common.redis.RedissonWxRedisOps;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @Author hupengming
 * @ClassName
 * @Date 2022-11-15 11:48
 * @Version 1.0
 */
@Service
public class RedissonWxRedisOpsService {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private String port;

    @Value("${spring.redis.database}")
    private Integer database;

    @Value("${spring.redis.password:null}")
    private String password;

    public RedissonClient redissonClient() {
        Config config = new Config();
        SingleServerConfig singleServerConfig = config.useSingleServer();
        String schema = "redis://" + host + ":" + port;
        singleServerConfig.setAddress(schema);
        singleServerConfig.setDatabase(database);
        if (StringUtils.isNotEmpty(password)) {
            singleServerConfig.setPassword(password);
        }
        return Redisson.create(config);
    }

    public RedissonWxRedisOps redissonWxRedisOpsClient() {
        return new RedissonWxRedisOps(redissonClient());
    }

}
